import java.util.Arrays;

public class Solution59 {
    public int[][] generateMatrix(int n) {
        int[][] ans=new int[n][n];
        int[] dx=new int[]{0,1,0,-1};
        int[] dy=new int[]{1,0,-1,0};
        int d=0,cx=0,cy=-1;
        for (int i=1;i<=n*n;i++){
            while (!valid(cx+dx[d],cy+dy[d],n) || ans[cx+dx[d]][cy+dy[d]]!=0){
                d=(d+1)%4;
            }
            cx=cx+dx[d];
            cy=cy+dy[d];
            ans[cx][cy]=i;
        }
        return ans;
    }

    boolean valid(int x,int y,int n){
        return x>=0 && x<n && y>=0 && y<n;
    }

    public static void main(String[] args) {
        int[][] ans=new Solution59().generateMatrix(3);
        for (int[] an : ans) {
            System.out.println(Arrays.toString(an));
        }
    }
}
